package com.example.tool.entity;

import com.example.tool.module.ModuleInformation;
import com.example.tool.utils.AES;

public class ConfigDBEntity {
  // 数据库类型
  private String dbType;
  // 数据库地址
  private String dbUrl;
  // 数据库用户名
  private String dbUser;
  // 数据库密码
  private String dbPassword;

  public ConfigDBEntity(String dbType, String dbUrl, String dbUser, String dbPassword) {
    this.dbType = dbType;
    this.dbUrl = dbUrl;
    this.dbUser = dbUser;
    this.dbPassword = dbPassword;
  }

  private ConfigDBEntity(String[] config) {
    if (config == null) {
      this.dbType = null;
      this.dbUrl = null;
      this.dbUser = null;
      this.dbPassword = null;
    } else {
      this.dbType = config[0];
      this.dbUrl = config[1];
      this.dbUser = config[2];
      this.dbPassword = config[3];
    }
  }

  public static ConfigDBEntity init(String base) throws Exception {
    if (!base.isEmpty()) {
      String decrypt = AES.decrypt(ModuleInformation.MODULE_SECRET_KEY, base);
      String[] split = new String[4];
      String[] strings = decrypt.split("\\|SEEYONDB\\|");
      for (int i = 0; i < split.length; i++) {
        if (i < strings.length) {
          split[i] = strings[i];
        } else {
          split[i] = null;
        }
      }
      return new ConfigDBEntity(split);
    }
    return null;
  }

  public String finalResult() throws Exception {
    return AES.encryption(ModuleInformation.MODULE_SECRET_KEY, this.toString());
  }

  public String getDbType() {
    return dbType;
  }

  public String getDbUrl() {
    return dbUrl;
  }

  public String getDbUser() {
    return dbUser;
  }

  public String getDbPassword() {
    return dbPassword;
  }

  public void setDbType(String dbType) {
    this.dbType = dbType;
  }

  public void setDbUrl(String dbUrl) {
    this.dbUrl = dbUrl;
  }

  public void setDbUser(String dbUser) {
    this.dbUser = dbUser;
  }

  public void setDbPassword(String dbPassword) {
    this.dbPassword = dbPassword;
  }

  @Override
  public String toString() {
    return dbType + "|SEEYONDB|" + dbUrl + "|SEEYONDB|" + dbUser + "|SEEYONDB|" + dbPassword;
  }
}
